package com.innovation.chengzhangriji.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.ConfigurationManager;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteEditText;
import com.innovation.library.util.InnovationAlgorithm;

public class DeleteDialogActivity extends BaseActivity implements View.OnClickListener {

    private CuteEditText editPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_dialog);
        editPassword = (CuteEditText) findViewById(R.id.edit_password);
        CuteButton btnOk = (CuteButton) findViewById(R.id.btn_ok);
        CuteButton btnCancel = (CuteButton) findViewById(R.id.btn_cancel);
        btnOk.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                LoginInfo loginInfo = Config.getLoginInfo(this);
                if (loginInfo == null) {
                    return;
                }
                String phone = loginInfo.getPhone();
                String token = loginInfo.getToken().getAccessToken();
                String password = InnovationAlgorithm.SHA1(Config.getConfig(ConfigurationManager.PASSWORD_SALT), editPassword.getText().toString());
                if (TextUtils.isEmpty(password)) {
                    tipShow("请输入密码");
                    return;
                }

                Client.requestCheckPassword(this, phone, token, password, new ResponseHandler() {

                    @Override
                    public void onInnovationStart() {
                        showProgressDialog(null, null);
                    }

                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    public void onChZhRJFailure(String value) {
                        if (value.equals("用户不存在或者密码错误")) {
                            tipShow("密码输入不正确,请重新输入.");
                        }
                    }

                    @Override
                    public void onInnovationFinish() {
                        dismissProgressDialog();
                    }

                    @Override
                    public void onInnovationExceptionFinish() {
                        dismissProgressDialog();
                    }
                }, networkUnavailableSampleImpl());

                break;
            case R.id.btn_cancel:
                finish();
                break;
        }
    }
}
